<template>
  <div class="sidebar" :class="{ hidden: sidebarHidden }">
    <div class="menu-container">
      <div class="module-group" v-for="module in apiData" :key="module.category">
        <div class="module-header" @click="$emit('toggle-module', module.category)">
          <span>{{ module.category }}</span>
          <span class="expand-icon" :class="{ expanded: expandedModules[module.category] }">▶</span>
        </div>
        <div v-if="expandedModules[module.category] || module.apis.length <= 5">
          <div class="menu-item level-2" 
               v-for="api in module.apis" 
               :key="api.id"
               :class="{ active: api.id === currentApiId }"
               @click="$emit('show-api-detail', api, module)">
            {{ api.name || '未知接口' }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType } from 'vue';
import type { APIModule } from '../api/model';

export default defineComponent({
  name: 'Sidebar',
  props: {
    apiData: {
      type: Array as PropType<APIModule[]>,
      default: () => []
    },
    expandedModules: {
      type: Object as PropType<Record<string, boolean>>,
      default: () => ({})
    },
    sidebarHidden: {
      type: Boolean,
      default: false
    },
    currentApiId: {
      type: String,
      default: ''
    }
  },
  emits: ['toggle-module', 'show-api-detail']
});
</script>

<style scoped>
.sidebar {
    width: var(--sidebar-width);
    background-color: var(--sidebar-bg);
    border-right: 1px solid var(--border-color);
    transition: transform 0.3s ease;
    position: relative;
    display: flex;
    flex-direction: column;
    height: 100%;
    flex-shrink: 0;
    overflow: hidden;
}

.sidebar.hidden {
    transform: translateX(-100%);
    position: absolute;
    height: 100%;
    z-index: 100;
}

/* 确保侧边栏内的内容可以完全展开 */
.sidebar {
    display: flex;
    flex-direction: column;
    height: 100%;
}

.menu-container {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
}

.module-group {
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}

.module-header {
    padding: 12px 15px;
    cursor: pointer;
    font-weight: 500;
    color: var(--heading-color);
    background-color: var(--sidebar-bg);
    transition: background-color 0.2s;
    position: relative;
}

.module-header:hover {
    background-color: var(--highlight-bg);
    color: var(--theme-color);
}

.menu-item {
    padding: 8px 15px;
    cursor: pointer;
    transition: all 0.2s;
    position: relative;
    font-size: 14px;
    color: var(--text-color);
    border-left: 2px solid transparent;
}

.menu-item:hover {
    background-color: var(--highlight-bg);
    color: var(--theme-color);
}

.menu-item.active {
    background-color: var(--highlight-bg);
    color: var(--theme-color);
    border-left-color: var(--theme-color);
}

.menu-item.level-2 {
    padding-left: 30px;
    font-size: 13px;
    color: var(--text-muted);
}

.expand-icon {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    transition: transform 0.3s;
    font-size: 10px;
}

.expand-icon.expanded {
    transform: translateY(-50%) rotate(90deg);
}
</style>